package com.kevin.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kevin.service.entity.SysRole;
import com.kevin.service.entity.SysRoleMenu;
import com.kevin.service.entity.SysUserRole;
import com.kevin.service.mapper.SysRoleMapper;
import com.kevin.service.service.SysRoleMenuService;
import com.kevin.service.service.SysRoleService;
import com.kevin.service.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色管理(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2023-03-28 19:58:24
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    private final SysRoleMenuService roleMenuService;
    private final SysUserRoleService sysUserRoleService;

    @Override
    public void saveRole(SysRole entity) {
        // 保存角色
        baseMapper.insert(entity);

        // 保存角色菜单关系
        roleMenuService.saveOrUpdate(entity.getId(), entity.getMenuIdList());
    }

    @Override
    public void updateRole(SysRole entity) {
        // 更新角色
        updateById(entity);

        // 更新角色菜单关系
        roleMenuService.saveOrUpdate(entity.getId(), entity.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRole(List<Long> idList) {
        // 删除角色
        removeByIds(idList);

        // 删除用户角色关系
        sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getRoleId, idList));

        // 删除角色菜单关系
        roleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().in(SysRoleMenu::getRoleId, idList));

    }
}

